from pyparsing import Word, alphas, nums, oneOf, Group, Optional, Suppress, restOfLine

def split_questions_pyparsing(text):
    question_num = Word(nums) + Suppress(".")
    answer_option = oneOf(list("abcd")) + Suppress(")") + restOfLine
    question = Group(question_num + restOfLine + Optional(answer_option[1, ...]))
    parser = question[1, ...]

    parsed_questions = parser.parseString(text)
    questions = ["".join(q) for q in parsed_questions]
    return questions

# 示例试卷文本
exam_text = """
1. What is the capital of France?
   a) Berlin
   b) Madrid
   c) Paris
   d) Rome

2. Which planet is known as the Red Planet?
   a) Earth
   b) Mars
   c) Jupiter
   d) Saturn

3. Who wrote 'Pride and Prejudice'?
   a) William Shakespeare
   b) Jane Austen
   c) Charles Dickens
   d) Mark Twain
"""

# 划分题目
questions = split_questions_pyparsing(exam_text)
for i, question in enumerate(questions, 1):
    print(f"Question {i}:\n{question}\n")
